package org.paradise.dms.web.action.lodgemgr;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.AllocationInfo;
import org.paradise.dms.pojo.Dormitory;
import org.paradise.dms.services.DormitoryService;
import org.paradise.dms.services.StudentLodgeMgrService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.DWRPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 宿舍分配的action
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-23 下午07:59:54 李双江（paradise.lsj@gmail.com）created
 */
@Service
@RemoteProxy(name = "dormitoryAllocateAction")
public class DormitoryAllocateAction extends DMSBaseAction {

	private static Logger log = Logger.getLogger(DormitoryAllocateAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 7488406552194864278L;

	@Autowired
	private StudentLodgeMgrService studentLodgeService;
	@Autowired
	private DormitoryService dormitoryService;

	public StudentLodgeMgrService getStudentLodgeService() {
		return studentLodgeService;
	}

	public void setStudentLodgeService(StudentLodgeMgrService studentLodgeService) {
		this.studentLodgeService = studentLodgeService;
	}

	public DormitoryService getDormitoryService() {
		return dormitoryService;
	}

	public void setDormitoryService(DormitoryService dormitoryService) {
		this.dormitoryService = dormitoryService;
	}

	/**
	 * 
	 * Description:给单个学生和临时入住人员分配宿舍
	 * 
	 * @Version1.0 2009-3-28 下午09:30:13 李双江（paradise.lsj@gmail.com）创建
	 * @param tempid
	 * @param dormitoryid
	 * @param startindate
	 * @param endoutdate
	 * @return
	 * @throws DaoException
	 */
	public String allocateTEMP(String tempid, String dormitoryid, String startindate, String endoutdate) throws DaoException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date sdate = null;
		Date edate = null;
		try {
			sdate = df.parse(startindate);
			edate = df.parse(endoutdate);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		try {
			String statusMsg = this.studentLodgeService.allocateSingleStudent(Integer.parseInt(tempid), Integer.parseInt(dormitoryid), sdate, edate);
			if ("".equals(statusMsg)) {
				return "分配成功!";
			}
			if ("该学生已经分配宿舍".equals(statusMsg)) {
				return "该学生已经分配宿舍";
			}
			log.error(statusMsg);
			return "系统错误，请稍候再试！";
		} catch (NumberFormatException e) {
			log.error("DMS_error:参数格式错误 非int");
			log.error("DMS_error:失败原因" + e);
			return "系统错误，请稍候再试！";
		}

	}

	/**
	 * 
	 * Description: 返回所有的与分配标记值
	 * 
	 * @Version1.0 2009-4-5 上午01:17:23 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONArray getAllAllocationInfo() {
		List<AllocationInfo> aiList = studentLodgeService.getAllAllocationInfo();
		JSONArray allocationlist = new JSONArray().fromObject(aiList);
		return allocationlist;
	}

	/**
	 * 
	 * Description: 分页查询指定预分配标记的宿舍列表
	 * 
	 * @Version1.0 2009-4-5 下午01:03:38 李双江（paradise.lsj@gmail.com）创建
	 * @param pageNum
	 * @param totalCount
	 * @param dormitrypreallocationid
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONArray getDormitoryPerPageByPreID(int pageNum, int totalCount, String dormitrypreallocationid, HttpSession session) {
		List<Dormitory> preidDormList = new ArrayList<Dormitory>();
		int totalRow = totalCount;
		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 10);
		page.setCurrentPage(pageNum);
		session.setAttribute("dormpage", page);
		int cp = (page.getCurrentPage() - 1) * 10;
		int ps = page.getPageSize();
		preidDormList = dormitoryService.getDormitoryByDormitoryPreID(dormitrypreallocationid, ps, cp);
		if (preidDormList != null & preidDormList.size() > 0) {
			log.info("满足预分配标记的宿舍有：=" + preidDormList.size());
			JSONArray presdormjson = new JSONArray().fromObject(preidDormList);
			return presdormjson;
		}
		return null;
	}

	/**
	 * 
	 * Description:查询dormitorypreallocationid的宿舍的总行数
	 * 
	 * @Version1.0 2009-4-5 下午01:05:15 李双江（paradise.lsj@gmail.com）创建
	 * @param dormitrypreallocationid
	 * @return
	 */
	public int getDormitoryByDormitoryPreIDRows(String dormitrypreallocationid) {
		return dormitoryService.getDormitoryByDormitoryPreIDRows(dormitrypreallocationid);
	}

	/**
	 * 
	 * Description: 返回页面分页对象
	 * 
	 * @Version1.0 2009-4-5 下午01:06:25 李双江（paradise.lsj@gmail.com）创建
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject getDormPage(HttpSession session) {
		JSONObject joa = new JSONObject().fromObject((DWRPage) session.getAttribute("dormpage"));
		return joa;
	}

	/**
	 * 
	 * Description: 根据dormitoryid去查这个宿舍的可住人数
	 * 
	 * @Version1.0 Apr 7, 2009 5:53:56 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param dormitoryid
	 * @return
	 */
	public int getAvailBedNoByDormitoryID(String dormitoryid) {
		return dormitoryService.getAvailBedNoByDormitoryID(dormitoryid).size();
	}

	/**
	 * 
	 * Description: 返回满足特定dormitorypreid的dormitory列表
	 * 
	 * @Version1.0 May 20, 2009 10:11:40 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param dormitorypreid
	 * @return
	 */
	public String[][] getDormitoryIDListByDormitoryPreID(String dormitorypreid) {
		return dormitoryService.getDormitoryIDListByDormitoryPreID(dormitorypreid);
	}

	/**
	 * 
	 * Description:返回满足某一个preid的宿舍的总空闲入住人数
	 * 
	 * @Version1.0 May 21, 2009 11:22:53 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param dormitorypreid
	 * @return
	 */

	public int getAvailDormInAmountByDormitoryPreID(String dormitorypreid) {
		return dormitoryService.getAvailDormInAmountByDormitoryPreID(dormitorypreid);
	}
}
